﻿@{
    ViewBag.Title = "Index";
    Layout = null;
}
@using DDit.Core.Data.SystemEntity.Entity;
@using DDitApplicationFrame.Common;
@using DDit.Core.Data.Entity.CoreEntity;
@Styles.Render("~/Scripts/bootstrap/css/1", "~/Scripts/datatables/css/1", "~/Content/1", "~/Scripts/alertjs/themes/1", "~/Scripts/select2/css/1", "~/Scripts/iconpicker/css/1", "~/Scripts/fancyTree/skin-win7/1", "~/Scripts/fileUpload/1")
@Scripts.Render("~/bundles/js")

<script type="text/javascript">
    $(function () {
        //初始化查询部门
        $("#Groupid").select2({
            minimumResultsForSearch: -1,
            tags: "true",
            placeholder: "请选择",
            allowClear: true,
        }).val(null).trigger("change");
        //初始化表格配置
        var table = $('#userTable').custromTable({
            ajax: {
                url: "/WxUser/GetUserList",
                data: function (d) {
                    d.nickname = $("#nickname").val();
                    d.Groupid = $("#Groupid").val();
                    return JSON.stringify(d);
                }
            },
            columns: [
                { data: 'openid', visible: false },
                { data: 'nickname', title: "用户名" },
                { data: 'sex', title: "性别" },
                { data: 'Address', title: "所在地" },
                { data: 'remark', title: '备注' },
                { data: 'Group', title: '用户组' },
                //{
                //    title: '操作', render: function (data, type, row) {
                //        return '<button type="button" class="btn btn-info " onclick="modifyRemark()" > 修改备注  </button > ' +
                //               '<button type="button" class="btn btn-info " onclick="modifyRemark()" > 设置用户组  </button > ';
                //    }
                //}

            ]
        });
        $('#userTable tbody').on('dblclick', 'tr', function () {
            modifyRemark();
        });

        //新增或修改确认按钮
        var confrimBtn = function (index) {
            var openid = parent.$("#openid").val();
            var userForm = parent.$("#Userform");
            if (userForm.valid()) {
                var loading = parent.layer.load(2);
                userForm.ajaxSubmit({
                    dataType: "json",
                    success: function (data) {
                        parent.layer.close(loading);
                        if (data.result) {
                            parent.layer.close(index); //隐藏弹出
                            table.ajax.reload(null, openid == "" ? true : false);
                            alertify.success("操作成功！"); //弹出消息日志
                        } else {
                            alertify.error(data.message);
                        }
                    },
                    error: function (data) {
                        parent.layer.close(loading);
                        var obj = eval('(' + data.responseText + ')');
                        alertify.error(obj.message);
                    }
                });
            }
        }

        var openCreateUserLayer = function () {
            distriRoleLayer = parent.layer.open({
                type: 1,
                title: "修改用户备注",
                area: ['400px', '400px'],
                closeBtn: 1,
                shadeClose: false,
                skin: 'layerStyle',
                content: $("#wxuserLayer").html(),
                btn: [dditConfig.btn.close, dditConfig.btn.comfrie],
                yes: function (index, layero) {
                    confrimBtn(index);
                },
                success: function (i, l) {

                }
            });
        }

        //新增或修改确认按钮
        var modifyGroup = function (index) {
            var menu = table.rows('.selected');
            var model = menu.data();
            var lis = parent.$(".gbin1-list li")
            var mbobj = {};
            mbobj["openid"] = model[0].openid;
            if (lis.length > 0) {
                lis.each(function (i, v) {
                    mbobj["mplist[" + i + "].id"] = $(v).data("tag");
                });                
            }
            mbobj["oldTag"] = $("#lasttag").text();
            $.post("/WxUser/AddorEditUserGroup", mbobj, function (data) {
                if (data.result) {
                    alertify.success("分配成功！");
                    table.ajax.reload();
                }
                else {
                    alertify.error(data.message);
                }
            });
            parent.layer.close(index);
        }
        var openCreateGroupLayer = function () {
            distriRoleLayer = parent.layer.open({
                type: 1,
                title: "修改用户组",
                area: ['500px', '500px'],
                closeBtn: 1,
                shadeClose: false,
                skin: 'layerStyle',
                content: $("#wxGroupLayer").html(),
                btn: [dditConfig.btn.close, dditConfig.btn.comfrie],
                yes: function (index, layero) {
                    modifyGroup(index);
                },
                success: function (i, l) {
                    parent.$("#formJs").FromEvent(function () {
                        //分配按钮选中
                        $("#btnGroup button").on('click', function () {
                            var value = $(this).text();
                            var tag = $(this).data("tag");
                            $(this).toggleClass("btnn");
                            var li = $("li[data-tag='" + tag + "']");
                            if (li.length > 0)
                            { li.remove() } else {
                                if ($(".gbin1-list li").length == 0) {
                                    $("#orderContain").show();
                                }
                                $("<li data-tag='" + tag + "'>" + value + "</li>").insertBefore($(".gbin1-list").find("div"));
                                $('.gbin1-list').sortable();
                            }
                        })
                    });
                }
            });
        }

        var modifyRemark = function () {
            var cc = table.rows('.selected')
            if (cc[0].length > 0) {
                var model = cc.data();         // 得到操作的实体
                openCreateUserLayer();
                parent.$("#Userform").setForm(model[0]);  //给表单赋值
                parent.$("#nickname").val(model[0].nickname);
                parent.$("#remark").val(model[0].remark);
                parent.$("#UserImg").attr('src', model[0].headimgurl.substring(0, model[0].headimgurl.length - 1));
                parent.$("#nickname").attr("readonly", "readonly");

            } else {
                alertify.log("请选择一条操作信息！");
            }
        }
        //编辑按钮操作
        $("#wxuserremark").click(function () {
            var cc = table.rows('.selected')
            if (cc[0].length > 0) {
                var model = cc.data();         // 得到操作的实体
                openCreateUserLayer();
                parent.$("#Userform").setForm(model[0]);  //给表单赋值
                parent.$("#nickname").val(model[0].nickname);
                parent.$("#remark").val(model[0].remark);
                parent.$("#UserImg").attr('src', model[0].headimgurl.substring(0, model[0].headimgurl.length - 1));
                parent.$("#nickname").attr("readonly", "readonly");

            } else {
                alertify.log("请选择一条操作信息！");
            }
        });

        //修改用户组
        $("#wxusersetGroup").click(function () {
            var cc = table.rows('.selected')
            if (cc[0].length > 0) {
                var model = cc.data();         // 得到操作的实体
                $("#disnickname").text(model[0].nickname);
                openCreateGroupLayer();
                var oldTag = "";
                if (model[0].UserGroup.length > 0) {
                    parent.$("#orderContain").show()
                    $.each(model[0].UserGroup, function (i, v) {
                        oldTag += v.id + ",";
                        var cc = parent.$("#btnGroup button").filter("[data-tag='" + v.id + "']");
                        if (cc.length > 0) {
                            $(cc).addClass("btnn");
                            parent.$("<li data-tag='" + v.id + "'>" + $(cc).text() + "</li>").insertBefore(parent.$(".gbin1-list div"));
                            if (model[0].UserGroup.length == i + 1) {
                                parent.$('.gbin1-list').sortable();
                            }
                        }
                    });
                }
                $("#lasttag").text(oldTag);
            } else {
                alertify.log("请选择一条操作信息！");
            }
        });

        //拉黑用户
        $("#wxuserpullback").click(function () {
            var cc = table.rows('.selected')
            if (cc[0].length > 0) {
                var model = cc.data();
                parent.layer.confirm("确定要拉黑该用户！", dditConfig.hintLayer, function () {
                    $.post("/WxUser/PullBackUser", { openid: model[0].openid }, function (data) {
                        if (data.result) {
                            cc.remove().draw(false);
                            parent.layer.close(parent.layer.index);
                            alertify.success("拉黑成功！");
                        } else {
                            alertify.error(data.message);
                        }
                    });
                });
            } else {
                alertify.log("请选择一条操作信息！");
            }
        });

        //查询按钮
        $("#userQuery").click(function () {
            table.ajax.reload();
        })
    })

</script>
<div class="margin w100f xoverhide">
    <div class="panel panel-default" style="margin-top: 10px">
        @Html.Action("CreateButtonByMuen", "Button", new { menuId = 45, mark = "wxuser" })
    </div>
    <form class="form-inline">
        <div class="form-group mleft10">
            <label for="Groupid">用户组</label>
            <select name="Groupid" id="Groupid" class="form-control" style="width:150px">
                @foreach (var item in ViewBag.Group as List<UserGroup>)
                {
                    <option value="@item.id">@item.name</option>
                }
            </select>
        </div>
        <button type="button" class="btn btn-info " id="userQuery">
            <span class="glyphicon glyphicon-search" aria-hidden="true"></span> 查询
        </button>
    </form>

    <table id="userTable" class="table table-bordered table-striped w100f" cellspacing="0"></table>
</div>

<div id="wxuserLayer" class="displayn">
    <form class="form-horizontal formPadding" id="Userform" method="post" action="/WxUser/AddorEditUserInfo">
        @Html.Hidden("openid")
        <div class="form-group ">
            <img id="UserImg" style="margin-left:80px" width="200" height="150" />
        </div>
        <div class="form-group">
            <label for="UserReallyname" class="col-sm-2 control-label labsty">昵称<span style="color:red">*</span></label>
            <div class="col-sm-6">
                <input type="text" class="form-control" name="nickname" maxlength="10" id="nickname" placeholder="昵称">
            </div>
        </div>

        <div class="form-group">
            <label for="Remark" class="col-sm-2 control-label labsty">备注&nbsp;&nbsp;</label>
            <div class="col-sm-10 pad-r20">
                <input type="text" class="form-control" name="remark" maxlength="200" id="remark">
            </div>
        </div>

    </form>
</div>

<div id="wxGroupLayer" class="displayn">
    <form id="formJs"></form>

    @Html.Hidden("openid")
    <div class="alert alert-success" role="alert">
        昵称：<span id="disnickname"></span>
        <span style="visibility:collapse" id="lasttag"></span>
    </div>
    <div class="container-fluid" style="height:200px" id="btnGroup">
        <div class="row">
            @foreach (var item in ViewBag.Group as List<UserGroup>)
            {
                <div class="col-xs-3" style="margin-bottom:10px">
                    <button type="button" class="btn btn-default" data-tag="@item.id">
                        @item.name
                    </button>
                </div>
            }
        </div>
    </div>
    <div id="orderContain">
        <ul class="gbin1-list">
            <div style="clear:both;"></div>
        </ul>
    </div>
</div>